package com.ztjava.common.controller;

import com.jfinal.aop.Duang;
import com.jfinal.kit.Ret;
import com.ztjava.common.model.flow.FlowInfo;
import com.ztjava.common.service.FlowService;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.client.PortalClient;
import com.ztjava.core.tool.ConfigTool;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.ext.auth.bean.AuthSession;

@ControllerClass(client=PortalClient.class,path="/common/flow")
@UserController(portal=Constant.P_PORTAL)
public class FlowPortalController extends BaseController{
	private static FlowService flowService = Duang.duang(FlowService.class);

	/*************
	 *修改Flow
	**************/
	@UserMethod("CommonFlowInfo")
	public void addEditInfo(){
		Integer id = getParaToInt("id",0);
		FlowInfo flowInfo = new FlowInfo();
		if(id>0){
			flowInfo = FlowInfo.dao.findById(id);
		}
		setAttr("flowInfo",flowInfo);
		render("addEditInfo.html");
	}

	/*************
	 *保存Flow
	**************/
	@UserMethod("CommonFlowInfo")
	public void saveInfo(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		FlowInfo flowInfo = getModel(FlowInfo.class);
		Ret ret = flowService.checkRepeat(flowInfo,FlowInfo.tablename
			,""		,new String[]{"code","编码不能重复！"});
		if(ret.get("result").equals("error")){
			renderError(ret.get(Constant.MESSAGE).toString());
			return;
		}
		saveModel(flowInfo,authSession.getId());
		flowService.loadInfoCache(flowInfo.getInt("id"));
		ConfigTool.dbToBaseData("COMMON_FLOW", "select code as value,name,enable_flag from "
				+ FlowInfo.tablename + " order by id asc");
		renderSuccess();
	}
	/*************
	 *Flow列表
	**************/
	@UserMethod("CommonFlowInfo")
	public void infoList(){
		if(!getPara("is_ajax","F").equals("T")){
			render("infoList.html");
			return;
		}
		renderPageJson(flowService.getInfoPage(client, getParaMap()));
	}

	/*************
	 *批量删除Flow
	**************/
	@UserMethod("CommonFlowInfo")
	public void batchDeleteInfo(){
		String[] btSelectItem = getParaValues("btSelectItem");
		flowService.batchDelete(FlowInfo.tablename, btSelectItem);
		flowService.loadInfoCache();
		ConfigTool.dbToBaseData("COMMON_FLOW", "select code as value,name,enable_flag from "
				+ FlowInfo.tablename + " order by id asc");
		renderSuccess();
	}

	@UserMethod("CommonFlowInfo")
	public void designFlow(){
		Integer id = getParaToInt("id");
		FlowInfo flowInfo = FlowInfo.dao.findById(id);
		setAttr("flowInfo", flowInfo);
		render("designFlow.html");
	}
	
	@UserMethod("CommonFlowInfo")
	public void saveDesignFlow(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		FlowInfo flowInfo = getModel(FlowInfo.class);
		saveModel(flowInfo, authSession.getId());
		flowService.loadInfoCache(flowInfo.getInt("id"));
		renderSuccess();
	}
}
